<?php

namespace app\admin\service;

use app\admin\model\Prescription;
use think\facade\Db;

/**
 * 岗位管理-服务类
 * @author 小码哥
 * @since 2023/11/14
 * Class PositionService
 * @package app\admin\service
 */
class PrescriptionService extends BaseService
{
    /**
     * 构造函数
     * @author 小码哥
     * @since 2024/11/14
     *
     * PositionService constructor.
     */
    public function __construct()
    {
        $this->model = new Prescription();
    }

    /**
     * @return array
     * @throws \think\db\exception\DbException
     * 获取列表
     */
    public function getList()
    {
        $sort = 'id desc';
        // 常规查询条件
        $param = request()->param();
        $map = [];
        if ($param) {
            // 筛选标题
            if (isset($param['title']) && $param['title']) {
                $map[] = ['title', 'like', "%{$param['title']}%"];
            }
            if (isset($param['topic_id']) && $param['topic_id']) {
                $map[] = ['topic_id', '=', $param['topic_id']];
            }
            // 筛选状态
            if (isset($param['status']) && $param['status']) {
                $map[] = ['status', '=', $param['status']];
            }
            if (isset($param['type']) && $param['type']) {
                $map[] = ['type', '=', $param['type']];
            }
        }
        // 设置查询条件
        if (is_array($map)) {
            $map[] = ['mark', '=', 1];
        } elseif ($map) {
            $map .= " AND mark=1 ";
        } else {
            $map .= " mark=1 ";
        }
        $count = $this->model
            ->where($map)
            ->count();
        $ids = $this->model
            ->where($map)
            ->order($sort)
            ->page(PAGE, PERPAGE)
            ->column("id");
        $map[] = ['id', 'in', $ids];
        $field = '';
        $list = $this->model->getAllInfo($map, $field);
        if (empty($list)) {
            //获取数据总数
            //返回结果
            $message = array(
                "msg" => '操作成功',
                "code" => 0,
                "data" => $list,
                "count" => $count,
            );
            unset($list, $count, $map, $param, $ids);
            return $message;
        } else {
            $topic_ids = array_column($list, 'topic_id');
            $topicInfo = Db::name('topic_data')
                ->whereIn('id', $topic_ids)
                ->field('id,title,prescript_num')
                ->select()
                ->toArray();
            foreach ($list as &$item) {
                $item['prescript_num'] = 0;
                $item['topic_name'] = '';
                foreach ($topicInfo as $topic) {
                    if ($item['topic_id'] == $topic['id']) {
                        $item['topic_name'] = $topic['title'];
                        $item['prescript_num'] = $topic['prescript_num'];
                    }
                }
            }
            $message = array(
                "msg" => '操作成功',
                "code" => 0,
                "data" => $list,
                "count" => $count,
            );
            unset($list, $count, $map, $param, $ids);
            return $message;
        }
        //获取数据总数
        //返回结果

    }

    /**
     * 获取处方下拉列表
     * 抽取处方然后在处方中在选择对应的题目
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 小码哥
     * @since 2023/11/14
     * 筛选处方列表
     */
    public function getPrescriptionSelectList()
    {
        $param = request()->param();
        $type = request()->param('type');
        $map[] = ['status', '=', 1];
        $map[] = ['mark', '=', 1];
        $topic_id = isset($param['topic_id']) ? (int)$param['topic_id'] : '';
        if ($topic_id) {
            $map[] = ['topic_id', '=', $topic_id];
        }
        if ($type) {
            $map[] = ['type', '=', $type];
        }
        $list = $this->model->where($map)
            ->order("id", "desc")
            ->select()
            ->toArray();
        return message("操作成功", true, $list);
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2023/11/28
     * @author 小码哥
     */
    public function edit()
    {
        //接收参数
        $data = request()->param();
        $id = request()->param('id');
        $all_score = Db::name('topic_data')
            ->where('id', $data['topic_id'])
            ->value('all_score');
        $type = Db::name('topic_data')
            ->where('id', $data['topic_id'])
            ->value('type');
        $data['all_score'] = $all_score;
        $data['type'] = $type;
        $initTopicId = 0;
        if ($id) {
            $initTopicId = Db::name('prescription')
                ->where('id', $id)
                ->value('topic_id');
        }
        $res = parent::edit($data);
        $this->uptopicNum($data['topic_id'], $initTopicId);
        return $res; // TODO: Change the autogenerated stub
    }

    /**
     * 更新题库下的处方数量
     */
    public function uptopicNum($topic_id, $initTopicId)
    {
        if ($initTopicId == 0) {
            $num = Db::name('prescription')
                ->where('topic_id', $topic_id)
                ->count('*');
            Db::name('topic_data')
                ->where('id', $topic_id)
                ->update(['prescript_num' => $num]);
        } else {
            $num = Db::name('prescription')
                ->where('topic_id', $topic_id)
                ->count('*');
            $initTopicIdnum = Db::name('prescription')
                ->where('topic_id', $initTopicId)
                ->count('*');
            Db::name('topic_data')
                ->where('id', $topic_id)
                ->update(['prescript_num' => $num]);
            Db::name('topic_data')
                ->where('id', $initTopicId)
                ->update(['prescript_num' => $initTopicIdnum]);
            return;
        }

    }

}